/*
 * FileChooserDelegate.cpp  0.4.0 / Dec 04, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "FileChooserDelegate.h"

#include "ComponentDelegate.h"

// FileChooserDelegate :: Static Native Calls. ---------------------------------

/*!
 * \brief Returns directory selected by the user.
 *
 * \param env the JNIEnv pointer
 * \param cls the FileChooser java class
 * \param parentDelegate the parent delegate
 * \param caption the dialog title
 * \param directory the dialog directory
 * 
 * \return directory selected by the user
 */
jstring FileChooserDelegate::getExistingDirectoryName(JNIEnv *env, jclass cls,
                                                      jlong parentDelegate,
                                                      jstring caption,
                                                      jstring directory)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(FileChooserDelegate, getExistingDirectoryName,
                                jstring, SWING_ENV_ARG, JCLASS_ARG(cls),
                                JLONG_ARG(parentDelegate), JSTRING_ARG(caption),
                                JSTRING_ARG(directory))
  QWidget *parent = 0;
  if (parentDelegate) {
    parent = COMPONENT_CAST(parentDelegate)->qWidget();
  }
  QString selectedDirectory = QFileDialog::getExistingDirectory(parent,
                                               QStringAdapter(env, caption),
                                               QStringAdapter(env, directory));
  if (selectedDirectory.isEmpty()) {
    return 0;
  }
  return QStringAdapter::toJString(env, selectedDirectory);
}

/*!
 * \brief Returns file selected by the user.
 *
 * \param env the JNIEnv pointer
 * \param cls the FileChooser java class
 * \param parentDelegate the parent delegate
 * \param caption the dialog title
 * \param directory the dialog directory
 * \param filter the file filter
 * \param selectedFilter the selected filter
 * 
 * \return file selected by the user
 */
jstring FileChooserDelegate::getOpenFileName(JNIEnv *env, jclass cls,
                                             jlong parentDelegate,
                                             jstring caption, jstring directory,
                                             jstring filter,
                                             jstring selectedFilter)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(FileChooserDelegate, getOpenFileName,
                                jstring, SWING_ENV_ARG, JCLASS_ARG(cls),
                                JLONG_ARG(parentDelegate), JSTRING_ARG(caption),
                                JSTRING_ARG(directory), JSTRING_ARG(filter),
                                JSTRING_ARG(selectedFilter))
  QWidget *parent = 0;
  if (parentDelegate) {
    parent = COMPONENT_CAST(parentDelegate)->qWidget();
  }
  QStringAdapter currentFilter(env, selectedFilter);
  QString selectedFile = QFileDialog::getOpenFileName(parent,
                                          QStringAdapter(env, caption),
                                          QStringAdapter(env, directory),
                                          QStringAdapter(env, filter),
                                          &currentFilter, 0);
  if (selectedFile.isEmpty()) {
    return 0;
  }
  return QStringAdapter::toJString(env, selectedFile);
}

/*!
 * \brief Returns file selected by the user.
 *
 * \param env the JNIEnv pointer
 * \param cls the FileChooser java class
 * \param parentDelegate the parent delegate
 * \param caption the dialog title
 * \param directory the dialog directory
 * \param filter the file filter
 * \param selectedFilter the selected filter
 * 
 * \return file selected by the user
 */
jstring FileChooserDelegate::getSaveFileName(JNIEnv *env, jclass cls,
                                             jlong parentDelegate,
                                             jstring caption, jstring directory,
                                             jstring filter,
                                             jstring selectedFilter)
{
  Q_ASSERT(env);
  SWING_STATIC_REENTRANT_RETURN(FileChooserDelegate, getSaveFileName,
                                jstring, SWING_ENV_ARG, JCLASS_ARG(cls),
                                JLONG_ARG(parentDelegate), JSTRING_ARG(caption),
                                JSTRING_ARG(directory), JSTRING_ARG(filter),
                                JSTRING_ARG(selectedFilter))
  QWidget *parent = 0;
  if (parentDelegate) {
    parent = COMPONENT_CAST(parentDelegate)->qWidget();
  }
  QStringAdapter currentFilter(env, selectedFilter);
  QString selectedFile = QFileDialog::getSaveFileName(parent,
                                          QStringAdapter(env, caption),
                                          QStringAdapter(env, directory),
                                          QStringAdapter(env, filter),
                                          &currentFilter, 0);
  if (selectedFile.isEmpty()) {
    return 0;
  }
  return QStringAdapter::toJString(env, selectedFile);
}
